﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Drawing = System.Drawing;

namespace Cryomancer.StarChart
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		#region Members
		#endregion

		#region Properties
		#endregion

		#region C'Tors and object lifetime code
		public Window1()
		{
			InitializeComponent();

			SetMainDetailControl();

			VSPAccessMaster.ChartReady += new EventHandler<VSPChartReadyEventArgs>(VSPAccessMaster_ChartReady);
		}
		#endregion

		#region Methods
		private void SetMainDetailControl()
		{
			VSPChartDetail detail = new VSPChartDetail();
			detail.Name       = "W UMa";
			detail.FOV        = 180;
			detail.Resolution = 125;
			detail.MagLimit   = 12;
			detail.North      = NorthOrientation.Up;
			detail.East       = EastOrientation.Left;
			detail.OtherVariables = MarkOtherVariables.None;

			this.MainDetailCtrl.Detail = detail;
		}

		private void GetNewChart(VSPChartDetail detail)
		{
			this.MainChartView.Enabled = false;

			VSPAccessMaster.GetChartAsync(detail);
			
		}
		#endregion

		#region Event Handlers
		private void MainChartView_ChartZoom(object sender, ChartZoomRoutedEventArgs e)
		{
			try
			{
				VSPChartDetail detail = this.MainDetailCtrl.Detail.GetDuplicate();

				double zoomFactor = 60;
				if (e.ZoomDirection == ChartZoomRoutedEventArgs.Direction.In)
				{
					while ((detail.FOV - zoomFactor) < 0)
					{
						zoomFactor /= 2;
					}
					
					detail.FOV -= zoomFactor;
				}
				else
				{
					if (detail.FOV == 900)
					{
						return;
					}

					if ((detail.FOV + zoomFactor) > 900)
					{
						detail.FOV = 900;
					}
					else
					{
						detail.FOV += zoomFactor;
					}
				}

				GetNewChart(detail);
			}
			finally
			{
			}
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			GetNewChart(this.MainDetailCtrl.Detail);
		}

		private void VSPAccessMaster_ChartReady(object sender, VSPChartReadyEventArgs e)
		{
			VSPChartDetail detail = e.Detail;
			if (detail.ChartImage == null)
			{
				return;
			}

			this.MainDetailCtrl.Detail      = detail;
			this.MainChartView.CurrentChart = detail;

			this.MainChartView.Enabled = true;

			try
			{
				EquitorialCoordinate coord = ChartOCRMaster.GetCoordinate((Bitmap)detail.ChartImage);
				string               id    = ChartOCRMaster.GetChartID((Bitmap)detail.ChartImage);
			}
			catch
			{
			}
		}
		
		#endregion
	}
}
